package com.xf2048.view;

import com.xf2048.global.GameConfig;
import com.zhuifeng.study2048.R;
import com.xf2048.ui.MainActivity;
import com.xf2048.ui.WelcomeActivity;

import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.method.ScrollingMovementMethod;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class GameWinDialog extends Dialog {

	private Context context = null;
	private Button pictureButton = null;
	private TextView titleTextView = null;
	private TextView bodyTextView = null;
	private Button restartButton = null;
	private Button exitButton = null;
	private int studyNum;

	public GameWinDialog(Context context, int studyNum) {
		super(context, R.style.dialog);
		this.context = context;
		this.studyNum = studyNum;
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		super.setContentView(R.layout.game_win_dialog_view);

		this.init();
		this.setTextView();
	}

	private void init() {
		this.pictureButton = (Button) findViewById(R.id.game_win_dialog_view_picture_Button);
		this.titleTextView = (TextView) findViewById(R.id.game_win_dialog_view_title_TextView);
		this.bodyTextView = (TextView) findViewById(R.id.game_win_dialog_view_body_TextView);
		this.bodyTextView.setMovementMethod(ScrollingMovementMethod
				.getInstance());
		this.restartButton = (Button) findViewById(R.id.game_win_dialog_view_restart_Button);
		this.exitButton = (Button) findViewById(R.id.game_win_dialog_view_exit_Button);

		this.restartButton.setOnClickListener(new MyOnClickListener());
		this.exitButton.setOnClickListener(new MyOnClickListener());

	}

	class MyOnClickListener implements View.OnClickListener {

		@Override
		public void onClick(View v) {
			if (v == restartButton) {
				((MainActivity) context).restartGame();
				GameWinDialog.this.dismiss();
			} else if (v == exitButton) {
				((MainActivity) context).restartGame();
				Intent intent = new Intent(context, WelcomeActivity.class);
				context.startActivity(intent);
				((MainActivity) context).finish();
				GameWinDialog.this.dismiss();
			}
		}
	}

	private void setTextView() {
		int pictureId = 0;
		if (GameConfig.playMode == 1) {
			pictureId = getContext().getResources().getIdentifier(
					"n" + studyNum, "drawable", getContext().getPackageName());
		} else if (GameConfig.playMode == 2) {
			pictureId = getContext().getResources().getIdentifier(
					"nn" + studyNum, "drawable", getContext().getPackageName());
		} else if (GameConfig.playMode == 3) {
			pictureId = getContext().getResources().getIdentifier(
					"nl" + studyNum, "drawable", getContext().getPackageName());
		}
		int titleId = getContext().getResources().getIdentifier(
				"title_" + GameConfig.playMode + "_" + studyNum, "string",
				getContext().getPackageName());
		int bodyId = getContext().getResources().getIdentifier(
				"body_" + GameConfig.playMode + "_" + studyNum, "string",
				getContext().getPackageName());
		String title = getContext().getString(titleId);
		String body = getContext().getString(bodyId);

		this.pictureButton.setBackgroundResource(pictureId);
		this.titleTextView.setText(title);
		this.bodyTextView.setText(body);
	}
}
